// pages/intergralrecord/intergralrecord.js
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    /**总积分 */
    bonuspoints: 0,
    integralRecord: [],
    arrlength: 0,
    windowHeight: 1106,
    showRule: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.getConfig().isFormal) {
      this.setData({
        showRule: true
      })
    }
    if (app.globalData.windowHeight) {
      var height = app.globalData.windowHeight;
      this.data.windowHeight = height - 153;
      this.setData({
        windowHeight: this.data.windowHeight
      })
    }
    if (GlobalData.homeData && GlobalData.homeData.bonuspoints)
      this.data.bonuspoints = GlobalData.homeData.bonuspoints;
    this.setData({
      bonuspoints: this.data.bonuspoints,
    })
    // if (GlobalData.integralRecord.length)
    //   this.integralRecord = GlobalData.integralRecord;
    // else {
    dataManager.rigisterObserver("getbonuspointsflow", this.onComplete, this);
    dataManager.getIntegralRule(1, 20, false);
  },
  onComplete() {
    this.data.integralRecord = GlobalData.clone(GlobalData.integralRecord);
    // console.log(this.data.integralRecord)
    // for (let i = 0; i < this.data.integralRecord.length;i++){
    //   if (this.data.integralRecord[i].bonuspoints==20){
    //     this.data.integralRecord.splice(i,1)
    //     i=i-1
    //   }
    // }
    // console.log(this.data.integralRecord)
    if (GlobalData.integralRecord.length == this.data.arrlength) return;
    for (let i = 0; i < this.data.integralRecord.length; i++) {
      this.data.integralRecord[i].createtime = this.data.integralRecord[i].createtime.replace("T", " ")
    }
    this.data.loading = false;
    this.data.arrlength = this.data.integralRecord.length;
    this.setData({
      integralRecord: this.data.integralRecord,
      arrlength: this.data.arrlength,
    })
  },
  /**积分规则 */
  onIntegralRule() {
    if (app.getConfig().isFormal) {
      dataManager.UseNavigateTo("/pages/integralrule/integralrule", false)
    }

    // this.router.navigateByUrl("integralrule" + GlobalData._random())
  },
  /**触底加载更多数据 */
  onScrollSlide() {
    if (!this.data.loading) {
      this.data.loading = true;
      GlobalData.recordinfo.index += 1;
      GlobalData.recordinfo.add = true;
      dataManager.rigisterObserver("getbonuspointsflow", this.onComplete, this);
      dataManager.getIntegralRule(GlobalData.recordinfo.index, GlobalData.recordinfo.size, GlobalData.recordinfo.add);
    }

  },
  /**滑动加载更多记录 */
  lower: function (e) {
    if (!this.data.loading) {
      this.data.loading = true;
      GlobalData.recordinfo.index += 1;
      GlobalData.recordinfo.add = true;
      dataManager.rigisterObserver("getbonuspointsflow", this.onComplete, this);
      dataManager.getIntegralRule(GlobalData.recordinfo.index, GlobalData.recordinfo.size, GlobalData.recordinfo.add);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})